package ar.com.spsolutions.splilabrary.view.pages.user;

import java.util.List;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.com.spsolutions.splibrary.general.RoleConverterService.RolesEnum;
import ar.com.spsolutions.splibrary.services.UserService;
import ar.com.spsolutions.splibrary.services.dto.CRUDUserDTO;
import ar.com.spsolutions.splibrary.user.User;
import ar.com.spsolutions.splibrary.user.role.UserRole;
import ar.com.spsolutions.splilabrary.view.components.CRUDComponent;
import ar.com.spsolutions.splilabrary.view.version2.components.MainContainer;

public class UserModificationComponent extends MainContainer {

	private static final long serialVersionUID = 9105754606374040899L;
	private User selected;
	private Form<User> modifyForm;

	private String password2;
	@SpringBean
	private CRUDUserDTO crudUserDTO;

	public List<User> getAllUsers() {
		return this.getUserService().findAll();
	}

	protected UserService getUserService() {
		return this.getEntityService().getUserService();
	}

	@Override
	protected void addComponents() {
		final CRUDComponent<User> crudComponent = new CRUDComponent<User>(
				"userCRUD", "header", "footer", "data", this.getCrudUserDTO(),
				new String[] { "name:true", "email" });
		crudComponent.addEmail("email", "emailLabel");
		crudComponent.addPassword("password", "passwordLabel");
		crudComponent.addPassword("passwordConfirmation",
				"passwordConfirmationLabel");
		crudComponent.addComboChoice("role", "rolesLabel", RolesEnum.ROLE_LIBRARIAN);
		this.add(crudComponent);

	}

	// --accessors--
	public User getSelected() {
		return this.selected;
	}

	public void setSelected(final User selected) {
		this.selected = selected;
	}

	public Form<User> getModifyForm() {
		return this.modifyForm;
	}

	public void setModifyForm(final Form<User> modifyForm) {
		this.modifyForm = modifyForm;
	}

	public String getPassword2() {
		return this.password2;
	}

	public void setPassword2(final String password2) {
		this.password2 = password2;
	}

	public UserRole getSelectedRole() {
		if (this.getSelected() == null) {
			return null;
		}
		return this.getSelected().getRole();
	}

	public void setSelectedRole(final UserRole selectedRole) {
		if (this.getSelected() != null) {
			this.getSelected().setRole(selectedRole);
		}
	}

	public CRUDUserDTO getCrudUserDTO() {
		return this.crudUserDTO;
	}

	public void setCrudUserDTO(final CRUDUserDTO crudUserDTO) {
		this.crudUserDTO = crudUserDTO;
	}

}
